// pages/my/address/address.js
var app = getApp();
var dealtype;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    blankshow: false,
    blanktips: '~没有收货地址~'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    dealtype = '';
    if (options.dealtype) {
      if (options.dealtype == 'selected') {
        dealtype = options.dealtype;
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.showLoading();
    app.ready(() => {
      this.loadAddressList();
    });
    app.error(err => {
      if (!app.globalData.userWxInfo) {
        wx.navigateTo({
          url: '/pages/auth/auth',
        });
      }
    });
  },
  loadAddressList: function () {
    var url = '/index.php/api/User/address_list';
    app.request(url).then(this.addressListCallBack.bind(this))
  },
  addressListCallBack: function (res) {
    wx.hideLoading();
    // console.log(res);
    var tmplist = res.result;
    if (tmplist.length <= 0) {
      this.setData({
        blankshow: true,
      });
      return;
    }
    var addresslist = [];
    for (var i = 0; i < tmplist.length; i++) {
      var addressinfo = {};
      addressinfo.addressid = tmplist[i].address_id;
      addressinfo.name = tmplist[i].consignee;
      addressinfo.phone = tmplist[i].mobile;
      addressinfo.card = tmplist[i].idCard;
      addressinfo.province = tmplist[i].province;
      addressinfo.city = tmplist[i].city;
      addressinfo.district = tmplist[i].district;
      if (addressinfo.card == null) {
        addressinfo.card = '';
      }
      addressinfo.address = tmplist[i].address;
      addressinfo.isdefault = tmplist[i].is_default.toString();
      addresslist.push(addressinfo);
    }
    this.setData({
      blankshow: false,
      addresslist: addresslist
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  addNewAddress: function () {
    wx.navigateTo({
      url: './add/add',
    })
  },
  addressTap: function (event) {
    console.log(dealtype);
    var address = event.currentTarget.dataset.address;
    if(dealtype == 'selected'){
      dealtype = '';
      app.globalData.address.status = 1;
      app.globalData.address.value = address;
      wx.navigateBack({});
      return;
    }
    wx.navigateTo({
      url: './add/add?address=' + JSON.stringify(address),
    });
  }
})